package com.yykj.hospital.server.service.impl;

import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.util.PageRequestUtil;
import com.yykj.common.vo.PageData;
import com.yykj.hospital.server.form.HospitalNoticeEditForm;
import com.yykj.hospital.server.form.HospitalNoticeListForm;
import com.yykj.hospital.server.model.entity.HospitalGuide;
import com.yykj.hospital.server.model.entity.HospitalNotice;
import com.yykj.hospital.server.model.repository.HospitalNoticeRepository;
import com.yykj.hospital.server.service.HospitalNoticeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class HospitalNoticeServiceImpl implements HospitalNoticeService {


    @Override
    public HospitalNotice editHospitalNotice(HospitalNoticeEditForm form) {
        HospitalNotice hospitalNotice;
        if (form.getType() == null) {
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        if (form.getId() != null && form.getId() > 0) {
            hospitalNotice = noticeRepository.findById(form.getId()).orElseThrow(() -> new NormalException(ResultEnums.NOT_EXIST));
        } else {
            hospitalNotice = new HospitalNotice();
        }
        BeanUtils.copyProperties(form, hospitalNotice);
        hospitalNotice = noticeRepository.save(hospitalNotice);
        return hospitalNotice;
    }

    @Override
    public void deleteHospitalNotice(Integer id) {
        noticeRepository.deleteById(id);
    }

    @Override
    public PageData<HospitalNotice> listHospitalNotice(HospitalNoticeListForm form) {

        Pageable pageable = PageRequest.of(PageRequestUtil.getPage(form.getCurrentPage()), PageRequestUtil.getSize(form.getPageSize()), Sort.by(Sort.Order.asc("sorting"), Sort.Order.desc("addTime")));

        Page<HospitalNotice> list = noticeRepository.findAll(new Specification<HospitalNotice>() {
            @Override
            public Predicate toPredicate(Root<HospitalNotice> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> predicateList = new ArrayList<>();
                predicateList.add(builder.equal(root.get("hospitalId"), form.getHospitalId()));
                if (!StringUtils.isEmpty(form.getKeyword())) {
                    predicateList.add(builder.like(root.get("title"), "%" + form.getKeyword() + "%"));
                }
                if (form.getStatus() != null) {
                    predicateList.add(builder.equal(root.get("status"), form.getStatus()));
                }
                if (form.getType() != null) {
                    predicateList.add(builder.equal(root.get("type"), form.getType()));
                }

                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        }, pageable);
        return PageData.formatList(list, form.getCurrentPage(), form.getPageSize());
    }

    private HospitalNoticeRepository noticeRepository;

    @Autowired
    public void setNoticeRepository(HospitalNoticeRepository noticeRepository) {
        this.noticeRepository = noticeRepository;
    }
}
